home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 22 / AACD 22.iso / AACD / Online / Apache / lib / php / Benchmark / Iterate.php next >
Encoding:
PHP Script  |  2001-03-06  |  2.9 KB  |  106 lines

  1. <?php
  2. //
  3. // +----------------------------------------------------------------------+
  4. // | PHP version 4.0                                                      |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997, 1998, 1999, 2000 The PHP Group                   |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 2.02 of the PHP license,      |
  9. // | that is bundled with this package in the file LICENSE, and is        |
  10. // | available at through the world-wide-web at                           |
  11. // | http://www.php.net/license/2_02.txt.                                 |
  12. // | If you did not receive a copy of the PHP license and are unable to   |
  13. // | obtain it through the world-wide-web, please send a note to          |
  14. // | license@php.net so we can mail you a copy immediately.               |
  15. // +----------------------------------------------------------------------+
  16. // | Authors: Sebastian Bergmann <sb@phpOpenTracker.de>                   |
  17. // +----------------------------------------------------------------------+
  18. //
  19. // $Id: Iterate.php,v 1.1 2000/09/29 20:22:36 chagenbu Exp $
  20. //
  21.  
  22.   require_once "Benchmark/Timer.php";
  23.  
  24.   /**
  25.   * Benchmark::Benchmark_Iterate
  26.   * 
  27.   * Purpose:
  28.   * 
  29.   *   Benchmarking
  30.   * 
  31.   * Example:
  32.   * 
  33.   *   $benchmark = new Benchmark_Iterate;
  34.   * 
  35.   *   $benchmark->run( "my_function", 100 );
  36.   * 
  37.   *   $result = $benchmark->get();
  38.   * 
  39.   * @author   Sebastian Bergmann <sb@phpOpenTracker.de>
  40.   * @version  $Revision: 1.1 $
  41.   * @access   public
  42.   */
  43.  
  44.   class Benchmark_Iterate extends Benchmark_Timer
  45.   {
  46.     // {{{ run()
  47.  
  48.     /**
  49.     * Benchmarks a function.
  50.     *
  51.     * @param  string  $function   name of the function to be benchmarked
  52.     * @param  int     $iterations number of iterations (default: 100)
  53.     * @access public
  54.     */
  55.  
  56.     function run( $function, $iterations = 100 )
  57.     {
  58.       for( $i = 1; $i <= $iterations; $i++ )
  59.       {
  60.         $this->set_marker( "start_" . $i );
  61.  
  62.         call_user_func( $function );
  63.  
  64.         $this->set_marker( "end_" . $i );
  65.       }
  66.     }
  67.  
  68.     // }}}
  69.     // {{{ get()
  70.  
  71.     /**
  72.     * Returns benchmark result.
  73.     *
  74.     * $result[ x            ] = execution time of iteration x
  75.     * $result[ "mean"       ] = mean execution time
  76.     * $result[ "iterations" ] = number of iterations
  77.     *
  78.     * @return array $result
  79.     * @access public
  80.     */
  81.  
  82.     function get()
  83.     {
  84.       $result = array();
  85.       $total = 0;
  86.  
  87.       $iterations = count( $this->markers ) / 2;
  88.  
  89.       for( $i = 1; $i <= $iterations; $i++ )
  90.       {
  91.         $time = $this->time_elapsed( "start_" . $i , "end_" . $i );
  92.  
  93.         $total = bcadd( $total, $time, 6 );
  94.         $result[ $i ] = $time;
  95.       }
  96.  
  97.       $result[ "mean" ] = bcdiv( $total, $iterations, 6 );
  98.       $result[ "iterations" ] = $iterations;
  99.  
  100.       return $result;
  101.     }
  102.  
  103.     // }}}
  104.   }
  105. ?>
  106.